import {observable, action} from 'mobx'
import {isAuthenticated,
    authenticateSuccess,
    setlocalStorage,
    dellocalStorage,
    getUser,
    logout} from '../utils/auth'

class AppStore {
  @observable isLogin = !!isAuthenticated()
  @observable users = []
  @observable loginUser = {}
  @observable user = getUser()
  @observable rudderList = []
  @observable rudder = {}
  @observable salesdata = {}
  @observable salespage = {}

  @observable defaultrudderId = ''

  
    @action toggleLogin(flag,info={}) {
        this.user = Object.assign({},this.user,info)
        if (flag) {
            setlocalStorage('user', JSON.stringify(info)) 
            authenticateSuccess(info.UserId)   // set token
            this.isLogin = true
        } else {
            logout()
            dellocalStorage('user')
            this.isLogin = false
        }
    }

    @action initRudder(info=[]){
      this.defaultrudderId = info && info.length > 0 ? info[0].value : ''
      this.defaultrudderName = info && info.length > 0 ? info[0].mean : ''
      this.rudderList = info
      this.rudder = info && info.length > 0 ? info[0] : {}
    }

    @action initsalesdata(info={}){
        this.salesdata = info
    }

    @action initsalespage(info=1){
        this.salespage = info
     }

}

export default new AppStore()
